﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GrewalCRM.BLL;
using GrewalCRM.Entities;
using GrewalCRM.Entities.Common;

public partial class CallSearchAdvance : Page
{
  CallLog_BLL callLogBll = new CallLog_BLL();
  bool isSaveSucessfull;
  int callIDSaved;

  #region Event Handlers
  protected void Page_Load(object sender, EventArgs e)
  {
    ucscCallSC.CallSearch += new UC_SearchCritetia.CallSearchHandler(ucscCallSC_CallSearch);
  }
  void ucscCallSC_CallSearch(List<CallLog> Calls)
  {
    gvAdvanceSearchResult.DataSource = Calls;
    gvAdvanceSearchResult.DataBind();
  }
  protected void gvAdvanceSearchResult_RowCommand(object sender, GridViewCommandEventArgs e)
  {
    GridViewRow currentRow = ((e.CommandSource as Control).NamingContainer) as GridViewRow;

    if (null != currentRow)
    {
      if (currentRow.RowType == DataControlRowType.DataRow && e.CommandName == "Save")
      {
        UC_AddCallUC ucacSaveCall = currentRow.FindControl("ucacSaveCall") as UC_AddCallUC;
        if (ucacSaveCall != null)
        {
          ucacSaveCall.SavingCallHandler += new UC_AddCallUC.SavingCall(ucacSaveCall_SavingCallHandler);
          ucacSaveCall.btnSave_Click(sender, e);
        }
      }
    }
  }
  protected void gvAdvanceSearchResult_RowDataBound(object sender, GridViewRowEventArgs e)
  {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
      GridViewRow currentRow = e.Row;
      int callID = Convert.ToInt32(gvAdvanceSearchResult.DataKeys[currentRow.RowIndex].Value);

      if (isSaveSucessfull && callID == callIDSaved)
        currentRow.BackColor = Color.PaleGreen;
      else if (!isSaveSucessfull && callID == callIDSaved)
        currentRow.BackColor = Color.LightPink;

      HiddenField hdnCallStatusID = currentRow.FindControl("hdnCallStatusID") as HiddenField;
      HiddenField hdnCallTypeID = currentRow.FindControl("hdnCallTypeID") as HiddenField;
      HiddenField hdnDealerID = currentRow.FindControl("hdnDealerID") as HiddenField;
      HiddenField hdnInstallerID = currentRow.FindControl("hdnInstallerID") as HiddenField;
      HiddenField hdnACBrandID = currentRow.FindControl("hdnACBrandID") as HiddenField;
      HiddenField hdnIsTempClosed = currentRow.FindControl("hdnIsTempClosed") as HiddenField;

      HiddenField hdnInstallerCommisionCR = currentRow.FindControl("hdnInstallerCommisionCR") as HiddenField;
      HiddenField hdnInstallerMaterialCR = currentRow.FindControl("hdnInstallerMaterialCR") as HiddenField;
      HiddenField hdnInstallerAcDR = currentRow.FindControl("hdnInstallerAcDR") as HiddenField;
      HiddenField hdnDealerAcDR = currentRow.FindControl("hdnDealerAcDR") as HiddenField;
      HiddenField hdnDealerAcCR = currentRow.FindControl("hdnDealerAcCR") as HiddenField;

      UC_AddCallUC ucacSaveCall = currentRow.FindControl("ucacSaveCall") as UC_AddCallUC;

      if (Convert.ToInt32(hdnCallStatusID.Value) == (int)CallStatus.Cancel)
      {
        Button btnSave = currentRow.FindControl("btnSave") as Button;
        if (btnSave != null)
          btnSave.Enabled = false;
      }


      if (ucacSaveCall != null)
      {
        ucacSaveCall.PopulateDealer();
        ucacSaveCall.PopulateACBrands();
        ucacSaveCall.populateCallType();
        ucacSaveCall.StatusUC.Bind();

        if (hdnCallStatusID != null && Convert.ToInt32(hdnCallStatusID.Value.Trim()) > 0)
        {
          if (Convert.ToInt32(hdnCallStatusID.Value.Trim()) > 0) ucacSaveCall.CallStatus = Convert.ToInt32(hdnCallStatusID.Value.Trim());
          if (Convert.ToDecimal(hdnInstallerCommisionCR.Value.Trim()) > 0) ucacSaveCall.StatusUC.InstallerCommisionCR = Convert.ToDecimal(hdnInstallerCommisionCR.Value.Trim());
          if (Convert.ToDecimal(hdnInstallerMaterialCR.Value.Trim()) > 0) ucacSaveCall.StatusUC.InstallerMaterialCR = Convert.ToDecimal(hdnInstallerMaterialCR.Value.Trim());
          if (Convert.ToDecimal(hdnInstallerAcDR.Value.Trim()) > 0) ucacSaveCall.StatusUC.InstallerAcDR = Convert.ToDecimal(hdnInstallerAcDR.Value.Trim());
          if (Convert.ToDecimal(hdnDealerAcDR.Value.Trim()) > 0) ucacSaveCall.StatusUC.DealerAcDR = Convert.ToDecimal(hdnDealerAcDR.Value.Trim());
          if (Convert.ToDecimal(hdnDealerAcCR.Value.Trim()) > 0) ucacSaveCall.StatusUC.DealerAcCR = Convert.ToDecimal(hdnDealerAcCR.Value.Trim());
          ucacSaveCall.StatusUC.ddlStatus_SelectedIndexChanged(null, new EventArgs());
        }

        if (hdnCallTypeID != null && Convert.ToInt32(hdnCallTypeID.Value.Trim()) > 0)
          ucacSaveCall.CallType = Convert.ToInt32(hdnCallTypeID.Value.Trim());

        if (hdnDealerID != null && Convert.ToInt32(hdnDealerID.Value.Trim()) > 0)
          ucacSaveCall.Dealer = Convert.ToInt32(hdnDealerID.Value.Trim());

        if (hdnInstallerID != null && Convert.ToInt32(hdnInstallerID.Value.Trim()) > 0)
          ucacSaveCall.Installer = Convert.ToInt32(hdnInstallerID.Value.Trim());

        if (hdnACBrandID != null && Convert.ToInt32(hdnACBrandID.Value.Trim()) > 0)
          ucacSaveCall.ACBrand = Convert.ToInt32(hdnACBrandID.Value.Trim());

        if (hdnIsTempClosed != null)
          ucacSaveCall.StatusUC.IsTempClosed = Convert.ToBoolean(hdnIsTempClosed.Value);
      }
    }
  }

  void ucacSaveCall_SavingCallHandler(bool isSaveSucessfully, int callID)
  {
    this.isSaveSucessfull = isSaveSucessfully;
    callIDSaved = callID;
    bindGrid();
  }
  protected void gvAdvanceSearchResult_PageIndexChanging(object sender, GridViewPageEventArgs e)
  {
    gvAdvanceSearchResult.PageIndex = e.NewPageIndex;
    bindGrid();
  }
  #endregion

  private void bindGrid()
  {
    ucscCallSC.btnSearch_Click(new object(), new EventArgs());
  }
}
